package co2.engine;

import java.io.IOException;
import java.util.Map;

import javax.faces.context.FacesContext;

import co2.bean.SignInBean;

public class SessionValidation {
	private static final String redirectLink = "/jsf/target/tumbal.jsf";

	public static SignInBean checkSession(){
		Map sessionMap = null;
		
		try{
			sessionMap = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
		
		}catch(Exception e){
			e.printStackTrace();
		}
		
		if(sessionMap!=null){
			SignInBean signInBean = (SignInBean) sessionMap.get("signInBean");
			if(signInBean!=null&&signInBean.getTheUser()!=null){
				return signInBean;
			}else{
				redirect();
				return null;
			}
		}else{
			redirect();
			return null;
		}
	}
	
	
	public static void redirect(){
		try {
			String path = FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath();
			FacesContext.getCurrentInstance().getExternalContext().redirect(path+redirectLink);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
